home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1999 / MacHack 1999.toast / The Hacks / Avi's Balloon Previewer / main.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-06-24  |  3.5 KB  |  159 lines  |  [TEXT/CWIE]

  1. // Avi's Balloon Previewer
  2. // by Avi Drissman <mailto:drissman@acm.org>
  3. // with a hell of a lot of help from Marshall Clow and Leonard Rosenthal
  4. // addl thanks to Eric Traut, Chris Russ
  5.  
  6. #include <SetUpA4.h>
  7. #include <A4Stuff.h>
  8. #include <Errors.h>
  9. #include <Balloons.h>
  10.  
  11. #include "formats.h"
  12.  
  13. typedef    pascal    void    (*BalloonsProcPtr) ();
  14. #define callBalloons(userRoutine)        \
  15.             (*(BalloonsProcPtr)(userRoutine))()
  16.  
  17. //prototypes
  18. //static asm    void    saveregisters (void);
  19. //static asm    void    restoreregisters (void);
  20. static pascal OSErr MyBalloons(ResType                 inType,
  21.                          SInt16                 inResID,
  22.                          SInt16                 inMessageIndex,
  23.                          SInt16                 inMessageState,
  24.                          HMMessageRecord *        outMessage,
  25.                          OSErr                    trapResult );
  26. static asm    void    checkconditions(void);
  27. static asm    void    originaladdress(void);
  28.  
  29. pascal void ShowInitIcon( short iconId, Boolean advance);
  30.  
  31. pascal void main()
  32. {
  33.     Handle     initHandle;
  34. //    OSErr    err;
  35.     
  36. //    if (Button())
  37. //        return;
  38.     
  39. //    EnterCodeResource();
  40. //    PrepareCallback();        // setup a4 for later usage!
  41.  
  42.     /* Locate this resource in memory and detach it */
  43.     initHandle = Get1Resource('INIT', 0);
  44.     HLock(initHandle);
  45.     HNoPurge(initHandle);
  46.     DetachResource(initHandle);
  47.  
  48.     long * saver = (long*)(&originaladdress);
  49.  
  50.     *saver = (long)NGetTrapAddress(_Pack14, ToolTrap);
  51.     ++saver;
  52.     *saver = (long)MyBalloons;
  53.  
  54. //    *((long*)(&originaladdress)) = (long)NGetTrapAddress(_Pack14, ToolTrap);
  55. //    *(((long*)(&originaladdress)) + 1) = (long)MyBalloons;
  56. //    gBalloonsAddress = (long)NGetTrapAddress(_Pack14, ToolTrap);
  57.     NSetTrapAddress((UniversalProcPtr)checkconditions, _Pack14, ToolTrap);
  58.  
  59.     ShowInitIcon(128, true);
  60.  
  61. //    ExitCodeResource();
  62. }
  63.  
  64. static pascal OSErr MyBalloons(ResType                 inType,
  65.                          SInt16                 /*inResID*/,
  66.                          SInt16                 /*inMessageIndex*/,
  67.                          SInt16                 /*inMessageState*/,
  68.                          HMMessageRecord *        outMessage,
  69.                          OSErr                    trapResult )
  70. {
  71. //    DebugStr("\pIn C patch.. Damn!");
  72.  
  73. //    Debugger();
  74.     
  75.     // Only for file balloons
  76.     if (inType != kHMFinderApplResType)
  77.         return trapResult;
  78.     
  79.     Handle pnotH = Get1IndResource('pnot', 1);
  80.     
  81.     // We need a preview
  82.     if (ResError() != noErr)
  83.         return trapResult;
  84.     
  85.     pnotStruct** thePnotH = reinterpret_cast<pnotStruct**>(pnotH);
  86.     
  87.     // A _picture_ preview
  88.     if (thePnotH [0]-> resType != 'PICT')
  89.         return trapResult;
  90.     
  91.     short pictID = thePnotH [0]-> resID;
  92.     
  93.     ReleaseResource (pnotH);
  94.     
  95.     outMessage -> hmmHelpType = khmmPict;
  96.     outMessage -> u.hmmPict = pictID;
  97.     
  98.     return noErr;    
  99. }
  100.  
  101. /*static asm    void    saveregisters (void)
  102. {
  103.     move.l    (sp)+,a0            // the return address in a0
  104.     movem.l    d0-d2/a1,-(sp)                // which is not saved
  105.     jmp        (a0)
  106. }
  107.  
  108. static asm    void    restoreregisters (void)
  109. {
  110.     move.l    (sp)+,a0            // the return address in a0
  111.     movem.l    (sp)+,d0-d2/a1                // which is not restored
  112.     jmp        (a0)
  113. }
  114. */
  115. static asm    void    checkconditions(void)
  116. {
  117.     cmp.w    #0x0711, d0 
  118.     beq.w    yup
  119.     lea        originaladdress, a0
  120.     move.l    (a0), a0
  121.     jmp (a0)
  122. yup:
  123. //    _Debugger
  124. //    Make a copy of the params
  125.     move.l    0x10(A7), -(A7)
  126.     move.l    0x10(A7), -(A7)
  127.     move.l    0x10(A7), -(A7)
  128.     move.l    0x10(A7), -(A7)
  129. //    call the orginal trap, and return to "returnHere"
  130.     pea        returnHere
  131.     lea        originaladdress, a0
  132.     move.l    (a0), a0
  133.     jmp (a0)
  134.     
  135.  
  136. returnHere:
  137. //    _Debugger
  138.     move.w    (A7)+, D0        // return value
  139.     move.w    D0, 0x12(A7)    // put it where the caller expects
  140.     beq.s    getOut            //    if there was no error, we're done
  141.     
  142.     move.l    (A7)+, A0    // return address
  143.     move.w    D0, -(A7)    //    the result from the 
  144.     move.l    a0, -(A7)
  145.     lea        originaladdress, a0 // MyBalloons
  146.     move.l    4(a0), a0
  147.     jmp     (a0)
  148.  
  149. getOut:
  150.     rtd       #0x0E
  151. }
  152.  
  153. static asm    void    originaladdress(void)
  154. {
  155.     _Debugger
  156.     _Debugger
  157.     _Debugger
  158.     _Debugger
  159. }